package com.authsphere.security.token.redis;

import com.authsphere.security.token.common.AccessToken;

import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/12 10:04
 **/
public class ExpirationAccessToken implements AccessToken, Serializable {
    @Serial
    private static final long serialVersionUID = 8940616957961931650L;


    private String value;

    private Instant expiration;
    private String tokenType = "Bearer";
    private boolean expired;
    private int expiresIn;

    private String seriesId;

    private Map<String, Object> additionalInformation = Collections.emptyMap();

    public ExpirationAccessToken(String value) {
        this.value = value;
    }

    public ExpirationAccessToken(AccessToken accessToken) {
        this.value = accessToken.getValue();
        this.tokenType = accessToken.getTokenType();
        this.additionalInformation = accessToken.getAdditionalInformation();
    }

    @Override
    public String getSeriesId() {
        return seriesId;
    }

    public void setSeriesId(String seriesId) {
        this.seriesId = seriesId;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return additionalInformation;
    }

    public void setAdditionalInformation(Map<String, Object> additionalInformation) {
        this.additionalInformation = additionalInformation;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    @Override
    public String getTokenType() {
        return tokenType;
    }

    public boolean isExpired() {
        return expiration != null && expiration.isBefore(Instant.now());
    }

    public Instant getExpiration() {
        return expiration;
    }

    public void setExpiration(Instant expiration) {
        this.expiration = expiration;
    }

    public int getExpiresIn() {
        return expiration != null ? Long.valueOf((expiration.getEpochSecond() - System.currentTimeMillis() / 1000))
                .intValue() : 0;
    }


    @Override
    public String getValue() {
        return value;
    }
}
